#!/bin/sh
#zyj log 
#date 2012-06-05
#author taoran.xu


PDATE=`/bin/date -d 'last day' +%F`


if [ $# -eq 1 ]; then
	PDATE="$1";
fi
PDATENO=`/bin/date -d  "$PDATE" +%Y%m%d`
QDATE=`/bin/date -d "$PDATE next day" +%F`

/server/bin/logs/bin/download_logs.pl -debug 5  -alertto 'data-request@qunar.com'   -date  "$QDATE"  -nodonefile -conf /server/bin/logs/etc/zyj.cfg
if [ $? -ne 0 ];then
       echo "Download zyj logs error:("
       exit 1
fi





LOGFILES="/logs/qunarlog/zyj/*/zyj.$PDATE.log.gz"
PSQLPATH="/export/tmp/zyjsql.sql"
/bin/zcat $LOGFILES |/bin/awk -v  pdate="$PDATE"  '                                                                                
        {
		if($3!="infinity"){$3=substr($3,1,length($3)-1);}
                if(NF==5)
                {
                        split($5,arr,";");
                        for(i in arr)  
                        {
                                str=$1" "$2" "$3" "$4" "arr[i];
                                cnt[str]++;
                        }
                } 
        }
        END {
                for(x in cnt)
                {
                        split(x,tmpArr," "); 
			sql=pdate""tmpArr[1]""tmpArr[2]""tmpArr[3]""tmpArr[4]""tmpArr[5]""cnt[x];
                        print sql; 
                }
        }' >$PSQLPATH
mysql -hl-dataware2.data.cn1 -ulog -p'log())(' -P5029 hotel --default-character-set=utf8 -e  "drop table if exists zyj_log_$PDATENO"
mysql -hl-dataware2.data.cn1 -ulog -p'log())(' -P5029 hotel --default-character-set=utf8 -e  "create table zyj_log_$PDATENO like zyj_log_template"
mysql -hl-dataware2.data.cn1 -ulog -p'log())(' -P5029 hotel --default-character-set=utf8 -e  "LOAD   DATA  LOCAL INFILE    '$PSQLPATH'     INTO   TABLE   zyj_log_$PDATENO   FIELDS   TERMINATED   BY  '' (reportdate,pflag,ptype,pvalue,pseq,pcode,pcnt)"
